﻿Imports System.IO
Imports System.Web
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
Imports MeoTom.Core.DAL

Namespace BLL
    Public Class Pages
        Public util As New Utilities.Common()

        Private _PageID As String
        Private _PageTitle As String
        Private _PageTitleNone As String
        Private _Status As Integer
        Private _CommentsEnabled As Boolean
        Private _ViewCount As Integer
        Private _CreatedDate As DateTime
        Private _ReleaseDate As DateTime
        Private _PagePath As String
        Private _PostBy As Integer
        Private _Parent As Integer
        Private _PostVisible As Integer
        Private _PagePassword As String
        Private _Body As String
        Private _TotalComment As Integer

        Public Property TotalComment() As Integer
            Get
                Return _TotalComment
            End Get
            Set(ByVal value As Integer)
                _TotalComment = value
            End Set
        End Property
        Public Property Body() As String
            Get
                Return _Body
            End Get
            Set(ByVal value As String)
                _Body = value
            End Set
        End Property
        Public Property PageID() As String
            Get
                Return _PageID
            End Get
            Set(ByVal value As String)
                _PageID = value
            End Set
        End Property
        Public Property PageTitle() As String
            Get
                Return _PageTitle
            End Get
            Set(ByVal value As String)
                _PageTitle = value
            End Set
        End Property
        Public Property PageTitleNone() As String
            Get
                Return _PageTitleNone
            End Get
            Set(ByVal value As String)
                _PageTitleNone = value
            End Set
        End Property
        Public Property Status() As Boolean
            Get
                Return _Status
            End Get
            Set(ByVal value As Boolean)
                _Status = value
            End Set
        End Property
        Public Property CommentsEnabled() As Boolean
            Get
                Return _CommentsEnabled
            End Get
            Set(ByVal value As Boolean)
                _CommentsEnabled = value
            End Set
        End Property
        Public Property ViewCount() As Integer
            Get
                Return _ViewCount
            End Get
            Set(ByVal value As Integer)
                _ViewCount = value
            End Set
        End Property
        Public Property CreatedDate() As DateTime
            Get
                Return _CreatedDate
            End Get
            Set(ByVal value As DateTime)
                _CreatedDate = value
            End Set
        End Property
        Public Property ReleaseDate() As DateTime
            Get
                Return _ReleaseDate
            End Get
            Set(ByVal value As DateTime)
                _ReleaseDate = value
            End Set
        End Property
        Public Property PagePath() As String
            Get
                Return _PagePath
            End Get
            Set(ByVal value As String)
                _PagePath = value
            End Set
        End Property
        Public Property PostBy() As Integer
            Get
                Return _PostBy
            End Get
            Set(ByVal value As Integer)
                _PostBy = value
            End Set
        End Property
        Public Property Parent() As Integer
            Get
                Return _Parent
            End Get
            Set(ByVal value As Integer)
                _Parent = value
            End Set
        End Property
        Public Property PostVisible() As Integer
            Get
                Return _PostVisible
            End Get
            Set(ByVal value As Integer)
                _PostVisible = value
            End Set
        End Property
        Public Property PagePassword() As String
            Get
                Return _PagePassword
            End Get
            Set(ByVal value As String)
                _PagePassword = value
            End Set
        End Property

        Public PagePrv As New PageProvider

        Public Sub New()
            PagePrv = New PageProvider()
        End Sub

        Public Sub Create()
            PagePrv.Create(Me)
        End Sub

        Public Sub Update()
            PagePrv.Update(Me)
        End Sub

        Public Sub Delete()
            PagePrv.Delete(Me)
        End Sub

        Public Sub UpdateArticlePath()
            PagePrv.UpdateArticlePath(Me)
        End Sub

        Public Sub UpdateViewCount()
            PagePrv.UpdateViewCount(Me)
        End Sub

        Public Function GetAll() As DataSet
            Return PagePrv.GetAll()
        End Function

        Public Sub GetByEdit()
            Try
                Dim reader As SqlClient.SqlDataReader = PagePrv.GetByEdit(Me)
                If reader.Read Then
                    PageID = reader("PageID").ToString()
                    PageTitle = reader("PageTitle").ToString()
                    PageTitleNone = reader("PageTitleNone").ToString()
                    Status = util.ConvertIntToBool(Integer.Parse(reader("Status").ToString()))
                    CommentsEnabled = Boolean.Parse(reader("CommentsEnabled").ToString())
                    ViewCount = Integer.Parse(reader("ViewCount").ToString())
                    CreatedDate = DateTime.Parse(reader("CreatedDate").ToString())
                    ReleaseDate = DateTime.Parse(reader("ReleaseDate").ToString())
                    PagePath = reader("PagePath").ToString()
                    PostVisible = Integer.Parse(reader("PostVisible").ToString())
                    PagePassword = reader("PagePassword").ToString()

                    Dim sFile As String = Configs.PageFile & "ListFile\" & Year(CreatedDate).ToString & "\" & Month(CreatedDate).ToString & "\" & Day(CreatedDate).ToString & "\" & PageID & ".xml"
                    If File.Exists(sFile) Then
                        Dim xmldoc As New XmlDocument()
                        xmldoc.Load(sFile)

                        Dim nodelist As XmlNodeList = xmldoc.SelectNodes("Pages/Page")
                        Dim nodedetail As XmlNode

                        nodedetail = nodelist.Item(0).SelectSingleNode("Body")
                        Body = nodedetail.InnerText
                    End If
                End If
                reader.Close()
            Catch ex As Exception
            End Try
        End Sub

        Public Sub GetAfterIns()
            Try
                Dim reader As SqlClient.SqlDataReader = PagePrv.GetAfterIns(Me)
                If reader.Read Then
                    PageID = reader("PageID").ToString()
                    PageTitle = reader("PageTitle").ToString()
                    PageTitleNone = reader("PageTitleNone").ToString()
                    Status = Boolean.Parse(reader("Status").ToString())
                    CommentsEnabled = Boolean.Parse(reader("CommentsEnabled").ToString())
                    ViewCount = Integer.Parse(reader("ViewCount").ToString())
                    CreatedDate = DateTime.Parse(reader("CreatedDate").ToString())
                    ReleaseDate = DateTime.Parse(reader("ReleaseDate").ToString())
                    PagePath = reader("PagePath").ToString()
                    PostVisible = Integer.Parse(reader("PostVisible").ToString())
                    PagePassword = reader("PagePassword").ToString()
                End If
                reader.Close()
            Catch ex As Exception
            End Try
        End Sub

        Public Sub WriteTop10Last()
            Dim strOut As New StringBuilder()
            Try
                strOut.Append("<div class=""otherBox fl""><h3>Pages</h3><div class=""hr fl"">&nbsp;</div><div class=""content-box""><ul>")
                Dim reader As SqlClient.SqlDataReader = PagePrv.GetTop10Last()
                While reader.Read()
                    strOut.Append("<li><a href=""" & reader("PagePath").ToString() & """ title=""" & reader("PageTitle").ToString() & """>" & reader("PageTitle").ToString() & "</a></li>")
                End While
                reader.Close()
                strOut.Append("</ul></div></div>")

                Dim writer As New StreamWriter(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_PageTop10Last.htm", False, System.Text.Encoding.UTF8)
                writer.Write(strOut.ToString())
                writer.Flush()
                writer.Close()
            Catch ex As Exception
            Finally
                strOut = Nothing
            End Try
        End Sub

        Public Sub ReadXmlById(ByVal dYear As String, ByVal dMonth As String, ByVal dDay As String, ByVal PageID As String)
            Try
                Select Case dDay
                    Case "01"
                        dDay = "1"
                    Case "02"
                        dDay = "2"
                    Case "03"
                        dDay = "3"
                    Case "04"
                        dDay = "4"
                    Case "05"
                        dDay = "5"
                    Case "06"
                        dDay = "6"
                    Case "07"
                        dDay = "7"
                    Case "08"
                        dDay = "8"
                    Case "09"
                        dDay = "9"
                    Case Else
                End Select

                Dim sPath As String = Configs.PageFile & "ListFile\" & dYear & "\" & dMonth & "\" & dDay & "\" & PageID & ".xml"
                If File.Exists(sPath) Then
                    Dim xmldoc As New XmlDocument()
                    xmldoc.Load(sPath)

                    Dim nodelist As XmlNodeList = xmldoc.SelectNodes("Pages/Page")

                    Dim nodedetail As XmlNode

                    nodedetail = nodelist.Item(0).SelectSingleNode("PageID")
                    Me.PageID = nodedetail.InnerText
                    nodedetail = nodelist.Item(0).SelectSingleNode("PageTitle")
                    Me.PageTitle = nodedetail.InnerText
                    nodedetail = nodelist.Item(0).SelectSingleNode("Body")
                    Me.Body = nodedetail.InnerText
                    nodedetail = nodelist.Item(0).SelectSingleNode("CommentEnabled")
                    Me.CommentsEnabled = Boolean.Parse(nodedetail.InnerText)
                    nodedetail = nodelist.Item(0).SelectSingleNode("ViewCount")
                    Me.ViewCount = Integer.Parse(nodedetail.InnerText)
                    nodedetail = nodelist.Item(0).SelectSingleNode("ReleaseDate")
                    Me.ReleaseDate = DateTime.Parse(nodedetail.InnerText)
                    nodedetail = nodelist.Item(0).SelectSingleNode("TotalComment")
                    Me.TotalComment = Integer.Parse(nodedetail.InnerText)
                    nodedetail = nodelist.Item(0).SelectSingleNode("ArticlePassword")
                    Me.PagePassword = nodedetail.InnerText
                    nodedetail = nodelist.Item(0).SelectSingleNode("PostVisible")
                    Me.PostVisible = nodedetail.InnerText
                    nodedetail = nodelist.Item(0).SelectSingleNode("ArticlePath")
                    Me.PagePath = nodedetail.InnerText
                    nodedetail = nodelist.Item(0).SelectSingleNode("Approved")
                    Me.Status = Boolean.Parse(nodedetail.InnerText)
                Else
                End If
            Catch ex As Exception
            End Try
        End Sub

        Public Function ReadTop10Last() As String
            Dim strOut As String = ""
            Try
                Dim reader As New StreamReader(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_PageTop10Last.htm", System.Text.Encoding.UTF8)
                strOut = reader.ReadToEnd()
                reader.Close()
            Catch ex As Exception
            End Try
            Return strOut
        End Function

        Public Sub WriteXMLByID(ByVal ID As String, ByVal day As Integer, ByVal month As Integer, ByVal year As Integer, ByVal Body As String)
            Try
                Dim strPath As String = ""

                If Not Directory.Exists(Configs.PageFile & "ListFile\" & year.ToString() & "\" & month.ToString() & "\" & day.ToString() & "\") Then
                    Directory.CreateDirectory(Configs.PageFile & "ListFile\" & year.ToString() & "\" & month.ToString() & "\" & day.ToString() & "\")
                End If

                strPath = Configs.PageFile & "ListFile\" & year.ToString() & "\" & month.ToString() & "\" & day.ToString() & "\" & ID & ".xml"

                Dim xmlWrite As New XmlTextWriter(strPath, System.Text.Encoding.UTF8)

                xmlWrite.WriteStartDocument()
                xmlWrite.WriteStartElement("Pages")
                xmlWrite.WriteStartElement("Page", vbNullString)
                xmlWrite.WriteElementString("PageID", Me.PageID)
                xmlWrite.WriteElementString("PageTitle", Me.PageTitle)
                xmlWrite.WriteElementString("Body", HttpContext.Current.Server.HtmlEncode(Body))
                xmlWrite.WriteElementString("CommentEnabled", Me.CommentsEnabled.ToString())
                xmlWrite.WriteElementString("ViewCount", Me.ViewCount.ToString())
                xmlWrite.WriteElementString("ReleaseDate", Me.ReleaseDate.ToString())
                xmlWrite.WriteElementString("TotalComment", Me.TotalComment.ToString())
                xmlWrite.WriteElementString("ArticlePassword", Me.PagePassword)
                xmlWrite.WriteElementString("PostVisible", Me.PostVisible.ToString())
                xmlWrite.WriteElementString("ArticlePath", Me.PagePath)
                xmlWrite.WriteElementString("Approved", Me.Status.ToString())
                xmlWrite.WriteEndElement()
                xmlWrite.WriteEndElement()
                xmlWrite.Formatting = Formatting.Indented

                xmlWrite.Flush()
                xmlWrite.Close()
            Catch ex As Exception
            End Try
        End Sub
    End Class
End Namespace
